 /**
 * Description: 系统用户控制器
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-10 下午 19:43:58
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-10   xk       0.0.1     Initial
 */
package com.metro.paperless.sys.controller;

 import com.github.pagehelper.PageInfo;
 import com.metro.core.annotation.BusinessDesc;
 import com.metro.core.page.Page;
 import com.metro.core.vo.CommonResult;
 import com.metro.core.vo.JsonResult;
 import com.metro.core.vo.QueryParams;
 import com.metro.core.vo.ResultCode;
 import com.metro.core.vo.inf.IResult;
 import com.metro.paperless.common.beans.constant.DicTypeEnum;
 import com.metro.paperless.common.utils.DicUtil;
 import com.metro.paperless.sys.beans.vo.SysUser;
 import com.metro.paperless.sys.beans.vo.SysUserRole;
 import com.metro.paperless.sys.service.inf.ISysRoleService;
 import com.metro.paperless.sys.service.inf.ISysUserRoleService;
 import com.metro.paperless.sys.service.inf.ISysUserService;
 import com.metro.paperless.web.controller.BaseController;
 import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.web.servlet.view.RedirectView;

 import javax.servlet.http.HttpServletRequest;

 /**
 * 系统用户控制器<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-10]
 */
@Controller
@RequestMapping("/sysuser")
public class SysUserController extends BaseController {
    //菜单编码
    private static final String menuCode = "sysuser";
    //模块描述
    private static final String MODULE_DESC = "系统用户";
    @Autowired
    private ISysUserService sysUserService;
     @Autowired
     private ISysRoleService sysRoleService;
     @Autowired
     private ISysUserRoleService sysUserRoleService;
    @Override
    protected String getViewDir() {
        return "sys/";
    }
    
    /**
     * 去查询系统用户
     * 
     * @param sysUser 系统用户页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询系统用户列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(SysUser sysUser) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.setViewName(getView("sysuser_list"));
            mav.addObject("searchEntity", sysUser);
            mav.addObject("departments", DicUtil.getLabelValueList(DicTypeEnum.DEPARTMENT));
            mav.addObject("roles", DicUtil.getLabelValueList(DicTypeEnum.role));
        } catch (Exception ex) {
            log.error("查询系统用户出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询系统用户
     *
     * @param sysUser 系统用户页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询系统用户列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(SysUser sysUser, Page page, QueryParams<SysUser> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(sysUser);
            PageInfo<SysUser> pageInfo = sysUserService.queryForPage(queryParams);
            //此处从用户表中移除super
            //SysUser superUser =  sysUserService.getById(952865050819956736L);
            //pageInfo.getList().remove(superUser);

            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询系统用户出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 去新增系统用户
     * 
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去新增系统用户")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "create")
    public ModelAndView createForm(Model model, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            initCreateOrUpdateFrom(mav, "create");
        } catch (Exception ex) {
            log.error("去新增系统用户出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }
    
    /**
     * 初始化新增系统用户所需参数
     * @author xk
     */
    private void initCreateOrUpdateFrom(ModelAndView mav, String action){
        mav.addObject("departments", DicUtil.getLabelValueList(DicTypeEnum.DEPARTMENT));
        mav.addObject("userStatus", DicUtil.getLabelValueList(DicTypeEnum.USER_LOCK_STATUS));
        mav.addObject("roles", DicUtil.getLabelValueList(DicTypeEnum.role));
        mav.addObject("action", action);
        mav.setViewName(getView("sysuser_form"));
    }
    
    /**
     * 初始化修改系统用户所需参数
     * @param mav
     * @param action
     */
    private void initCreateOrUpdateFromUpdate(ModelAndView mav, String action){
        mav.addObject("departments", DicUtil.getLabelValueList(DicTypeEnum.DEPARTMENT));
        mav.addObject("userStatus", DicUtil.getLabelValueList(DicTypeEnum.USER_LOCK_STATUS));
        mav.addObject("roles", DicUtil.getLabelValueList(DicTypeEnum.role));
        mav.addObject("action", action);
        mav.setViewName(getView("sysuser_form_update"));
    }
    
    /**
     * 新增系统用户
     * 
     * @param entity 系统用户页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增系统用户")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveSysUser(SysUser entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysUserService.save(entity);
            jr.setResult(res);
            log.debug("新增系统用户成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增系统用户出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除系统用户
     * 
     * @param id 系统用户页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除系统用户")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除系统用户id={}",id);
            IResult codeMsg = sysUserService.deleteByIds(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除系统用户出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去修改系统用户
     * 
     * @param id 系统用户页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去修改系统用户")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "edit/{id}", method = { RequestMethod.GET})
    public ModelAndView updateForm(@PathVariable Long id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            SysUser entity = sysUserService.getById(id);
            mav.addObject("entity", entity);
            initCreateOrUpdateFromUpdate(mav,"edit");
        } catch (Exception ex) {
            log.error("去修改系统用户", ex);
            mav.setView(new RedirectView("list"));
        }
        return mav;
    }

    /**
     * 修改系统用户
     * 
     * @param entity 系统用户页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改系统用户")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateSysUser(SysUser entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysUserService.update(entity);
            jr.setResult(res);
            log.debug("修改系统用户成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改系统用户出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
      * 去查看系统用户
      *
      * @param id 系统用户页面表单对象唯一标识
      * @return 结果视图
      * @author xk
      * @since [0.0.1]
      * @version [0.0.1, 2018-01-10]
      */
     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看系统用户详情")
     @RequiresPermissions(menuCode + ":view")
     @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
     public ModelAndView info(@PathVariable Long id) {
         ModelAndView mav= new ModelAndView();
         try {
             mav.addObject("entity", sysUserService.getById(id));
             mav.setViewName(getView("sysuser_info"));
         } catch (Exception ex) {
             log.error("查看系统用户出错", ex);
         }
         return mav;

     }

     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看系统用户详情")
     @RequiresPermissions(menuCode + ":view")
     @RequestMapping(value = "info.json/{id}", method = { RequestMethod.GET })
     @ResponseBody
     public JsonResult infoJson(@PathVariable Long id) {
         JsonResult jr = new JsonResult();
         try {
             jr.setData(sysUserService.getById(id));
         } catch (Exception ex) {
             log.error("查看系统用户出错", ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;

     }


     /**
      * 修改系统用户与角色对应关系
      */
     @RequestMapping(value = "createUserRole", method = { RequestMethod.POST})
     @ResponseBody
     public CommonResult<Object> createUserRole(SysUserRole entity) {
         JsonResult jr = new JsonResult();
         try {
             IResult codeMsg = sysUserRoleService.createUserRole(entity);
             log.debug("修改系统用户与角色对应关系结果：{}", codeMsg);
             jr.setResult(codeMsg);
         } catch (Exception ex) {
             log.error("修改系统用户与角色对应关系", ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }
    
}
